<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>新年flag</el-breadcrumb-item>
        <el-breadcrumb-item> flag 审核 </el-breadcrumb-item> 
      </el-breadcrumb> 
    </basic-container> 
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="form={}"
        @search-change="searchChange"   @selection-change="selectionChange" > 
        <!-- 自定义按钮 -->
       <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            plain
            @click.stop="editSelect(true)"
            >通过</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            plain
            @click.stop="editSelect(false)"
            >拒绝</el-button
          >
          <br />
          <el-tag> 已通过n条 未通过n条 待审核n条</el-tag>
        </template>
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-edit"
            type="text"
            size="small"
            @click.stop="adopt(scope)"
            >通过</el-button
          >
          <el-button
            icon="el-icon-delete"
            type="text"
            size="small"
            @click.stop="refuse(scope)"
            >拒绝</el-button
          >	
        </template>
        <template slot="last" slot-scope="scope">
          {{ scope.row.update_time }}
          <el-tag>{{ scope.row.edit_account_name }}</el-tag>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { activeFlag as baseURL,uploadFile } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "create_time&DESC",
        // state : "$in1,2,4"
      },
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: true,
        selection: true,
        border: true,
        column: [
          {
            label: "日期",
            prop: "update_time",
            type: "daterange",
            search: true,
          },
          {
            label: "用户id",
            prop: "user_id",
            type: "input",
            search: true,
            placeholder: "用户id或马甲id",
            rules: [
              {
                required: true,
                message: "用户id或马甲id必须填写",
              },
            ],
          },
          {
            label: "用户名称",
            prop: "user_name",
            type: "input",
            search: false,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
          {
            label: "内容",
            prop: "flag_text",
            type: "textarea",
            span: 13,
          }, 
          
          {
            label: "地区",
            prop: "area",
            type: "number",
            search: true,
          },
          {
            label: "审核状态",
            prop: "state",
            type: "select",
            hide: false,
            search: false,
            dicData: [
              {
                label: "<font style='color:red'>待审核</font>",
                value: 1,
              },
              {
                label: "已通过",
                value: 2,
              },
              {
                label: "未通过",
                value: 3,
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            slot:true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    //某个不通过
    async refuse(scope) {
      if(scope.row.state==3){
        return this.$message.error("已是该状态,不可重复修改");
      }
      let userInfo = this.$store.state.user.userInfo
      scope.row.state = 3
      scope.row.edittime = new Date();
      scope.row.edit_account_name = userInfo.username;
      let data = await common(`${baseURL.update}`, scope.row, null, "POST");
      this.getList()
    },
    //某个通过
    async adopt(scope) {
      if(scope.row.state==2){
        return this.$message.error("已是该状态,不可重复修改");
      }
      let userInfo = this.$store.state.user.userInfo
      scope.row.state = 2
      scope.row.edittime = new Date();
      scope.row.edit_account_name = userInfo.username;
      let data = await common(`${baseURL.update}`, scope.row, null, "POST");
      this.getList()
    },
    /**
     * 全选的通过或拒绝
     * type : 2表示通过,4表示拒绝
     */
    async editSelect(type) {
      if(this.checks == undefined || this.checks.length==0){
        return this.$message.error("请选择要修改的内容");
      }
       let arr = this.checks.filter(item=> item.state==(type ? 2 : 3));
      if(arr.length>0){
        return this.$message.error("选择内容存在该状态,不可修改");
      }
      let userInfo = this.$store.state.user.userInfo
      this.checks.forEach(item => {
        item.state = type ? 2 : 3
        item.edit_time = new Date()
        item.edit_account_name = userInfo.username;
      })
      let data = await common(`${baseURL.updateAll}`, this.checks, null, "POST");
      this.getList()
    },
    /**勾选所有 */
    selectionChange (row) {
      this.checks = row
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        {...this.params,...this.default},
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      
      params.state = params.states;
      delete params.states;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>